import os,time,json
from urllib.request import urlretrieve
from urllib.parse import urlencode
import requests

def getPage(page,key):
	'''根据关键词爬取百度图片网页信息'''
	params={
		'tn':'resultjson_com',
		'ipn':'rj',
		'ct':'201326592',
		'fp':'result',
		'queryWord':key,
		'cl':'2',
		'lm':'-1',
		'ie':'utf-8',
		'oe':'utf-8',
		'word':key,
		'nc':'1',
		'pn':str(page),
		'rn':'30',
		'gsm':'1e',
	}

	url='http://image.baidu.com/search/index?'+urlencode(params)
	try:
		res=requests.get(url)
		if res.status_code==200:
			return res.json()
			print("爬取成功，正在解析...")
		else:
			return None
	except Exception as err:
		print("爬取失败，原因是："+str(err))
		return None

def getImage(json,page):
	'''解析网页内容获取图片链接'''
	data=json.get('data')
	if data:
		no = 1
		for item in data:
			yield{
				'image':item.get('hoverURL'),
				'title':str(page-30+no),
			}
			no = no+1

def saveImage(item,key):
	image_url = item["image"]
	title = item["title"]
	if image_url!='' and image_url!=None:
		'''根据图片链接下载图片并保存'''
		path = "./百度爬取"+key+"图片"
		if not os.path.exists(path):
			os.makedirs(path)

		#图片路径的处理
		image_url = item.get("image")
		save_pic = path+"/"+image_url.split("/").pop()+".jpg"

		#存储图片
		try:
			headers = {
				'User-Agent':"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36",
				"Upgrade-Insecure-Requests":'1',
                "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                "Accept-Encoding":"gzip, deflate, sdch",
                "Accept-Language":"zh-CN,zh;q=0.8,en;q=0.6",
                "Cache-Control":"no-cache",
			}
			res = requests.get(image_url,headers=headers)

			with open(path+"/"+item.get("title").split(".").pop()+".jpg","wb") as file:
				file.write(res.content)
		except Exception as err:
			print("保存失败，原因是："+str(err))
			return None

def main(key):
	'''爬取前124张照片'''
	print("爬取 "+key+" 图片中...")
	no = 1
	for i in range(30,121,30):
		items = getImage(getPage(i,key),i)
		if items:
			for item in items:
				print("正在保存第"+str(no)+"张图片...")
				no = no+1
				saveImage(item,key)
				time.sleep(0.3)

	print("所有图片保存完毕，部分图片如果缺失，则为缺少URL无法保存")

if __name__ == '__main__':
	'''主程序'''
	while True:
		key = input("请输入要爬取图片的搜索关键词，默认保存124张（输入q退出）：")
		if key == 'q':
			break
		else:
			main(key)
